# -*- coding: utf-8 -*-

'''eof
name:近半年内经营地址变更次数
code:EG_AddressCountInHalfYear
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date


def get_date_list():
    try:
        now = datetime.now()
        aaa = [(now + pd.tseries.offsets.DateOffset(months=i)).strftime('%Y%m%d') for i in [-6, 0]]
        return aaa
    except:
        return None


def get_AddressCountInHalfYear():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        alter_list = FormatData("alterList")
        if alter_list == [{}]:
            return u'缺失值'
        else:
            if len(alter_list) >= 1:
                result_list = set()
                date_list = get_date_list()
                for i in alter_list:
                    alt_date = i.get("altDate")
                    alt_item = i.get("altItem")
                    if alt_date not in null_type_list:
                        if date_format(alt_date[0:10]) not in null_type_list and alt_item not in null_type_list :
                            alt_item = alt_item.strip()
                            if date_list[0] < date_format(alt_date[0:10]) <= date_list[1] and (u'经营场所变更' in alt_item or u'驻在地址变更' in alt_item):
                                result_list.add(alt_date + alt_item)
                            else:
                                result_list.add(0)
                        else:
                            result_list.add(None)
                    else:
                        result_list.add(None)
                if len(result_list) == 1:
                    if 0 in result_list:
                        return 0
                    elif None in result_list:
                        return u'缺失值'
                    else:
                        return len(result_list)
                else:
                    result_list1 = filter(lambda x: x is not None and x != 0, result_list)
                    return len(result_list1)
            else:
                return 0
    except:
        return u'缺失值'


result = get_AddressCountInHalfYear()

